package com.witframework.dynoengine.render.page;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.witframework.core.persistence.JdbcTools;
import com.witframework.core.persistence.PageSortModel;
import com.witframework.dynoengine.AbstractParamSetObject;
import com.witframework.dynoengine.MetaFactory;
import com.witframework.dynoengine.model.WitMetaColumn;
import com.witframework.dynoengine.model.WitMetaTable;
import com.witframework.dynoengine.render.ColumnComparator;
import com.witframework.dynoengine.render.MiniTemplator;
import com.witframework.dynoengine.render.Renderer;
import com.witframework.dynoengine.render.TempleteFactory;
import com.witframework.dynoengine.render.formater.CellFormat;
import com.witframework.dynoengine.render.formater.DefaultCellFormat;
import com.witframework.util.HtmlBuilder;
import com.witframework.util.StringTools;

public class ViewDataPageRender extends AbstractParamSetObject implements Renderer {	
	public String render() {	
		String tableId=(String)getParameter(Renderer.PARAM_tableId);
		String tableName=(String)getParameter(Renderer.PARAM_tableName);
		WitMetaTable mt=MetaFactory.getInstance().getMetaTableById(tableId);
		if(tableName==null){			
			tableName=mt.getTableName();
		}
		HttpServletRequest httpRequest=(HttpServletRequest)getParameter(Renderer.PARAM_httpRequest);		
		MetaFactory.getInstance().refresh();
		MiniTemplator templator=TempleteFactory.getInstance().getMiniTemplator("form/form.page");
		templator.setVariableOpt("tableName", tableName);
		templator.setVariableOpt("tableTitle", mt.getTableTitle());
		templator.setVariableOpt("contextPath", httpRequest.getContextPath());
		List<WitMetaColumn> metColumnList=MetaFactory.getInstance().getMetaFileds(tableName);
		WitMetaColumn[] metColumns=metColumnList.toArray(new WitMetaColumn[0]);
		Arrays.sort(metColumns,new ColumnComparator());
		HtmlBuilder html=new HtmlBuilder();		
		html.append("column_title");
		templator.setVariableOpt("search_items", html.toString());
		templator.addBlockOpt("hasSearch");
		if(templator.blockExists("column_title_block")){
			for(WitMetaColumn wmc:metColumns){
				if(StringTools.parseBoolean(wmc.getIsListable())
						&&!wmc.getColumnName().equalsIgnoreCase("id")
						){
									
					String column_title=wmc.getColumnTitle();					
					if(StringUtils.isEmpty(column_title)){
						column_title=wmc.getColumnName();
					}
					templator.setVariableOpt("column_title", column_title);
					String isSortable=wmc.getIsSortable();
					if(StringTools.parseBoolean(isSortable)){
						templator.setVariableOpt("nosort", "");
					}else{
						templator.setVariableOpt("nosort", "nosort");
					}
					templator.addBlock("column_title_block");
				}
			}			
		}	
		
		String pagination_pageno=httpRequest.getParameter("pagination_pageno");
		String pagination_pagesize=httpRequest.getParameter("pagination_pagesize");
		if(StringUtils.isEmpty(pagination_pageno)){
			pagination_pageno="1";
		}
		if(StringUtils.isEmpty(pagination_pagesize)){
			pagination_pagesize="10";
		}
		
		PageSortModel psm = new PageSortModel();
		psm.setPageSize(Integer.parseInt(pagination_pagesize));
		psm.setPageNo(Integer.parseInt(pagination_pageno));		
		List<Map> dataList=JdbcTools.pageQuery(psm, "select * from "+tableName);		
		if(templator.blockExists("row_block")){
			for(Map data:dataList){
				for(WitMetaColumn wmc:metColumns){
					if(StringTools.parseBoolean(wmc.getIsListable())
							&&!wmc.getColumnName().equalsIgnoreCase("id")
						){
						String column_name=wmc.getColumnName().toUpperCase();	
						String formatClass=wmc.getFormatClass();
						Object column_value=data.get(column_name);
						CellFormat cf=null;
						if(StringUtils.isNotEmpty(formatClass)){
							try {
								cf=(CellFormat)Class.forName(formatClass).newInstance();								
							} catch (Exception e) {								
							}
						}
						if(cf==null){
							cf=new DefaultCellFormat();
						}
						String cellText=cf.parse(column_value);
						templator.setVariableOpt("column_value", cellText);
						templator.addBlock("column_block");
					}
				}
				templator.addBlock("row_block");
			}
		}
		templator.setVariableOpt("total_size",String.valueOf(psm.getTotalRows()));
		templator.setVariableOpt("pagination_pageno",String.valueOf(psm.getPageNo()));
		templator.setVariableOpt("pagination_pagesize",pagination_pagesize);
		templator.setVariableOpt("pagination_pagecount",String.valueOf(psm.getPageCount()));
		
		
		return StringTools.compressHtml(templator.generateOutput());
		
	}
}
